//
//  PMShipOrderDetailViewController.m
//  ERP
//
//  Created by goldeney on 15/4/3.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "PMShipOrderDetailViewController.h"
#import "WebServiceCommonRequest.h"
#import "MJRefresh.h"
#import "TPShipOrderDetailViewCell.h"

#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"

@interface PMShipOrderDetailViewController ()

@property (nonatomic, strong) NSArray *shipValue;
@property (strong, nonatomic) NSMutableArray *resultData;

@end

@implementation PMShipOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.resultData = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    [self.tableView registerClass:[TPShipOrderDetailViewCell class] forCellReuseIdentifier:@"TPShipOrderDetailViewCell"];
    [self setupRefresh];
    
}

- (void)getPassingParameter:(id)object
{
    NSArray *value = object;
    self.shipValue = value;
    
    //标题设置 @类型(1:退回已签, 2:退回未签, 3:本日发货单, 4:所有发货单)
    NSInteger typeNumber = [value.firstObject integerValue];
    if(typeNumber == 1) self.title = @"退回已签发货单";
    else if(typeNumber == 2) self.title = @"退回未签发货单";
    else if(typeNumber == 3) self.title = @"本日发货单";
    else if(typeNumber == 4) self.title = @"所有发货单";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:0];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getNetWorkDataIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.resultData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.resultData.count + 10];
    [self getNetWorkDataIsReresh:NO state:stateNumber end:endNumber];
}

#pragma mark 网络获取数据

- (void)getNetWorkDataIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    NSString *jobOrder = [self.shipValue lastObject];
    NSNumber *typeNumber = [self.shipValue firstObject];
    
//    NSString *typeValue = nil;
//    if(typeNumber == 1) typeValue = @"退回已签";
//    else if(typeNumber == 2) typeValue = @"退回未签";
//    else if(typeNumber == 3) typeValue = @"本日发货单";
//    else if(typeNumber == 4) typeValue = @"所有发货单";
    
    //@类型(1:退回已签, 2:退回未签, 3:本日发货单, 4:所有发货单)
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"SCDD_SCGDJH_FWDXX"];
    [content setStationID:self.stationID];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"任务单编号" value:jobOrder];
    [content addParameterKey:@"类型" value:typeNumber];
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count > 0){
            if(isReresh) [self.resultData removeAllObjects];
            [self.resultData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
        } else if(isReresh){
            [self.tableView setTableViewNotDataDrawText:@"暂无发货单" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TPShipOrderDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TPShipOrderDetailViewCell"];
    
    NSDictionary *itemData = [self.resultData objectAtIndex:indexPath.row];
    
    NSString *fldbh = [itemData objectFormatForKey:@"发货单编号"];
    NSString *fldlx = [itemData objectFormatForKey:@"发货单类型"];
    if(!fldlx) cell.fhdbhLabel.text = fldbh;
    else cell.fhdbhLabel.text = [NSString stringWithFormat:@"(%@)%@",fldlx,fldbh];
    
    cell.gcsjLabel.text = [itemData objectFormatStringForKey:@"司机" defaults:@"未收录"];
    
    NSString *CPH = [itemData objectFormatStringForKey:@"车牌号" defaults:@"未知"];
    NSString *CH = [itemData objectFormatStringForKey:@"车辆编号" defaults:@"无"];
    cell.gcphLabel.text = [NSString stringWithFormat:@"%@(%@)",CPH,CH];
    
    NSInteger fhfl = [itemData objectFormatIntegerForKey:@"发货方量"];
    NSInteger qsfl = [itemData objectFormatIntegerForKey:@"签收方量"];
    NSInteger zcfl = [itemData objectFormatIntegerForKey:@"转出方量"];
    NSInteger thfl = [itemData objectFormatIntegerForKey:@"退回方量"];
    NSInteger zffl = [itemData objectFormatIntegerForKey:@"作废方量"];
    NSString *fllInfo = [NSString stringWithFormat:@"发货%ld方=签收%ld方+退回%ld方(转出%ld方+作废%ld方）",fhfl,qsfl,thfl,zcfl,zffl];
    cell.fhlLabel.text = fllInfo;
    
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
